package com.service;

import com.dao.FlightJdbc;
import com.model.Flight;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FlightService {
    FlightJdbc cj = new FlightJdbc();
    public List<Flight> show() {
        List<Flight> list = new ArrayList<Flight>();
        ResultSet rs = cj.show();
        try {
            while (rs.next()) {
                int id = rs.getInt("id");
                String num = rs.getString("num");
                String splace = rs.getString("splace");
                String splaces = rs.getString("splaces");
                String stime =  rs.getString("stime");
                String etime =  rs.getString("etime");
                String mprice = rs.getString("mprice");
                String seat = rs.getString("seat");
                String flightnum = rs.getString("flightnum");
                Flight flight = new Flight(id,num,splace,splaces,stime,etime,mprice,seat,flightnum);
                list.add(flight);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    public void add(String num,String splace,String splaces,String stime,String etime,String mprice,String seat,String flightnum) {
        cj.add(num,splace,splaces,stime,etime,mprice,seat,flightnum);
    }
    public void delete(String id) {
        cj.delete(id);
    }
    public Flight selectById(int id) throws SQLException {
        ResultSet rs = cj.selectById(id);
        Flight flight =null;
        while(rs.next()){
            String num = rs.getString("num");
            String splace = rs.getString("splace");
            String splaces = rs.getString("splaces");
            String stime = rs.getString("stime");
            String etime = rs.getString("etime");
            String mprice = rs.getString("mprice");
            String seat = rs.getString("seat");
            String flightnum = rs.getString("flightnum");
            flight = new Flight(rs.getInt("id"),num,splace,splaces,stime,etime,mprice,seat,flightnum);
        }
        return flight;
    }
    public void update(String id, String num, String splace, String splaces, String stime, String etime, String mprice, String seat, String flightnum){
        cj.update(id,num,splace,splaces,stime,etime,mprice,seat,flightnum);
    }
    public Flight toupdate(String id) throws SQLException {
        ResultSet rs = cj.show();
        Flight flight = null;
        while (rs.next()) {
            String num = rs.getString("num");
            String splace = rs.getString("splace");
            String splaces = rs.getString("splaces");
            String stime = rs.getString("stime");
            String etime = rs.getString("etime");
            String mprice = rs.getString("mprice");
            String seat = rs.getString("seat");
            String flightnum = rs.getString("flightnum");
            flight = new Flight(rs.getInt("id"),num,splace,splaces,stime,etime,mprice,seat,flightnum);
        }
        return flight;
    }
}




